var xweb = {}
xweb.os = {}
//索取设备信息判断是否是iOS
xweb.os.isIOS = /iOS|iPhone|iPad|iPod/i.test(navigator.userAgent);
//判断是否是android
xweb.os.isAndroid = !xweb.os.isIOS;
xweb.callbacks = {}

// commandName 行为  parameters 参数
xweb.takeNativeAction = function (telecomID, params) {
    console.log("xweb 执行 takenativeaction ")
    var request = {};
    request.telecomID = telecomID;
    request.params = params;
    if (window.xweb.os.isAndroid) {
        console.log("android take native action" + JSON.stringify(request));
        window.android.takeNativeAction(JSON.stringify(request));
    } else {
        window.webkit.messageHandlers.iOS.postMessage(JSON.stringify(request))
    }
}

// commandName 行为  parameters 参数
xweb.takeNativeByCallBack = function (telecomID, params, callback) {

    var callbackname = "nativetojs_callback_" + (new Date()).getTime() + "_" + Math.floor(Math.random() * 10000);
    xweb.callbacks[callbackname] = {callback: callback};
    var request = {};
    request.telecomID = telecomID;
    request.params = params;
    request.jsMethodID = callbackname;

    if (window.xweb.os.isAndroid) {
        console.log("android take native action" + JSON.stringify(request));
        window.android.takeNativeAction(JSON.stringify(request));
    } else {
        window.webkit.messageHandlers.iOS.postMessage(JSON.stringify(request))
    }
}

//原生调用JS方法
xweb.callback = function (callbackname, response) {
    var callbackobject = xweb.callbacks[callbackname];
    console.log("android take to js callbackname " + callbackname + " response + " + response);
    if (callbackobject !== undefined) {
        if (callbackobject.callback != undefined) {
            var ret = callbackobject.callback(response);
            if (ret === false) {
                return
            }
            delete xweb.callbacks[callbackname];
        }
    }
}

window.xweb = xweb;